package controllers

import (
	"app/models"
	"fmt"
	beego "github.com/beego/beego/v2/server/web"
	"io/ioutil"
	"net/http"
	"time"
)

type CrawlerController struct {
	beego.Controller
}

func (c *CrawlerController) Post() {
	context, _ := http.Get("https://so.gushiwen.cn/gushi/songsan.aspx")

	bytes, err := ioutil.ReadAll(context.Body)

	if err != nil {
		fmt.Println("ioutil.ReadAll err=", err)
		return
	}

	hrefList := models.PoemHrefList(string(bytes))

	str := GetPoemInfo(hrefList)

	c.Ctx.WriteString(str)
	c.StopRun()
}

func GetPoemInfo(hrefList []string) string {

	str := ""
	for _, v := range hrefList {
		context, _ := http.Get(v)

		bytes, err := ioutil.ReadAll(context.Body)

		if err != nil {
			fmt.Println("ioutil.ReadAll err=", err)
		}

		id := models.PoemInfo(string(bytes))
		fmt.Println("添加成功, id:" + string(id))
		time.Sleep(time.Second * 1)
	}

	return str
}
